function [x,y,u]=roms_kslice( rslice_obj, varargin)
% ROMS_KSLICE:  Returns sigma layer slice from ROMS Netcdf file.
%
% Returns a matrix containing a horizontal slice at a specified depth
% at a given time step from a SCRUM NetCDF file.  Regions of grid that
% are shallower than requested value are returned as NaNs.
%
% USAGE: [x,y,u]=roms_kslice ( rslice_obj, timestep, depth)
% USAGE: [x,y,u]=roms_kslice ( rslice_obj, varname, timestep, depth)
%    rslice_obj:     
%        global structure that contains a lot of needed information
%    varname:
%        If provided, then don't use the variable named in rslice_obj
%    time:    
%        time index, must be zero-based
%    klayer:   
%        index of sigma layer
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Name: rslice-0_9_2 $
% $Id: roms_kslice.m,v 1.1 2005/07/29 14:52:47 jevans Exp $
% AUTHOR:  johnevans@acm.org
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin == 3
	varname = rslice_obj.varname;
	timestep = varargin{1};
	klayer = varargin{2};
else
	varname = varargin{1};
	timestep = varargin{2};
	klayer = varargin{3};
end

ncfile =  rslice_obj.ncfile_list{rslice_obj.ncfile_index};
varinfo = nc_getvarinfo ( ncfile, varname );

%
% Default return values.
x = []; y = []; u = [];

%if (nargin~=3)
%  help roms_zslice; return
%end








x = rslice_obj.rslice_grid.x.data;
y = rslice_obj.rslice_grid.y.data;


[eta_rho_length, xi_rho_length] = size(x);
s_rho_length = varinfo.Size(2);
y_length = varinfo.Size(3);
x_length = varinfo.Size(4);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% reshape the depth such that each vertical profile is a column.
y_dim_name = varinfo.Dimension{3};
x_dim_name = varinfo.Dimension{4};
if ( strcmp(y_dim_name,'eta_rho') & strcmp(x_dim_name,'xi_rho') )
	;
elseif ( strcmp(y_dim_name,'eta_u') & strcmp(x_dim_name,'xi_u') )
	y = (y(:,1:xi_rho_length-1) + y(:,2:xi_rho_length))/2;
	x = (x(:,1:xi_rho_length-1) + x(:,2:xi_rho_length))/2;
elseif ( strcmp(y_dim_name,'eta_v') & strcmp(x_dim_name,'xi_v') )
	y = (y(1:eta_rho_length-1,:) + y(2:eta_rho_length,:))/2;
	x = (x(1:eta_rho_length-1,:) + x(2:eta_rho_length,:))/2;
else
	disp(sprintf('what are the dimensions??, %s and %s??', y_dim_name, x_dim_name ) );
	help roms_zslice;
	return;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Retrieve the requested variable at the given timestep.
u = nc_varget ( ncfile, varname, [timestep klayer 0 0],[1 1 -1 -1] );


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If there is a suitable mask variable, we will want to mask out certain cells.
% For now, mask out the land cells.
if ( strcmp(y_dim_name,'eta_rho') & strcmp(x_dim_name,'xi_rho') )
  %Get the mask_rho variable
   % [mask_rho, status] = nc_varget ( ncfile, 'mask_rho' );

    mask_inds = find(rslice_obj.rslice_grid.roms_grid.mask_rho==0);
    u(mask_inds) = NaN * ones(size(mask_inds));
elseif ( strcmp(y_dim_name,'eta_u') & strcmp(x_dim_name,'xi_u') )
  %
  %Get the mask_u variable
    %[mask_u, status] = nc_varget ( ncfile, 'mask_u' );
    mask_inds = find(rslice_obj.rslice_grid.roms_grid.mask_u==0);
    u(mask_inds) = NaN * ones(size(mask_inds));
elseif ( strcmp(y_dim_name,'eta_v') & strcmp(x_dim_name,'xi_v') )
  %Get the mask_u variable
    %[mask_v, status] = nc_varget ( ncfile, 'mask_v' );
    mask_inds = find(rslice_obj.rslice_grid.roms_grid.mask_v==0);
    u(mask_inds) = NaN * ones(size(mask_inds));
else
  disp(sprintf('what are the dimensions??, %s and %s??', y_dim_name, x_dim_name ) );
  help roms_zslice;
  return;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% all done with netcdf file operations
return
